import time
import wave

import pyaudio
def main():
    rate = 16000  # 采样频率
    format = pyaudio.paInt16  # 采样位
    channels = 1  # 声道数
    chunks = 1280  # 缓冲区
    p = pyaudio.PyAudio()
    stream = p.open(rate=rate, channels=channels, format=format, frames_per_buffer=chunks, input=True)
    wf=wave.open("TXT.pcm",'wb')
    wf.setnchannels(1)
    wf.setsampwidth(p.get_sample_size(pyaudio.paInt16))
    wf.setframerate(16000)
    print("开始录音")
    time_1=rate/chunks
    for i in range(int(time_1*5)):   #37次
        m=i%(int(time_1))
        if m==0 and i!=0:
            print("时间------->%d秒"%((i//12.5)+1))
        data=stream.read(chunks)
        wf.writeframes(data)
    print("录音结束")

    stream.stop_stream()
    stream.close()
    p.terminate()
    wf.close()
if __name__ == '__main__':
    main()